package com.lj.service;


import com.lj.dao.ArchiveRepository;
import com.lj.po.Blog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lj
 * @Description: ArchiveServiceImpl
 * @date 2020-10-05 22:02
 * @QQ 851477108
 */

@Service
@Transactional
public class ArchiveServiceImpl implements ArchiveService {
    @Autowired
    private ArchiveRepository archiveRepository;

    /**
     * Map<String,List<Blog>>,年份对应单个博客集合,先查询所有年份组,然后根据年份查询博客集,输入map中最终输出
     * @return
     */
    @Override
    public Map<String, List<Blog>> archiveBolg() {
        List<String> years = archiveRepository.findGroupYear();
        Map<String,List<Blog>> map = new HashMap<>();
        for (String year : years) {
            List<Blog> blogs = archiveRepository.findByYear(year);
            map.put(year,blogs);
        }
        return map;
    }

    /**
     * 查询归档中所有博客数
     * @return
     */
    @Override
    public Long countBlog() {
        return archiveRepository.count();
    }
}
